package com.example.demo.controller;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.custom.AjaxResult;
import com.example.demo.custom.JwtTokenUtils;
import com.example.demo.entity.Permission;
import com.example.demo.entity.TbMenu;
import com.example.demo.security.UserUtils;
import com.example.demo.service.PermissionService;
import com.example.demo.service.TbMenuService;

@RestController
@CrossOrigin

public class MainController {
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private PermissionService permissionService;
	@Autowired
	private TbMenuService menuService;
	
	@RequestMapping(value = "/loginSucc")
	public Object loginSucc(HttpServletResponse response) { 
		String roles = userUtils.getRoles();
		String username = userUtils.getUsername();
		for (String string : roles.split(",")) {
			if(string.replace(" ", "").contains("ROLE_LJH")) {
				String token = JwtTokenUtils.createToken(username,username,roles);
				 token=JwtTokenUtils.TOKEN_PREFIX+token;  //加上token前缀
				    HashMap<String, Object> result=new HashMap<String, Object>();
				    result.put("token",token);
				    result.put("role", roles);
				  return new AjaxResult("登录成功", result);
			}
		}
		String name = userUtils.getName();
		String token = JwtTokenUtils.createToken(username, name, roles);
		 token=JwtTokenUtils.TOKEN_PREFIX+token;  //加上token前缀
		    HashMap<String, Object> result=new HashMap<String, Object>();
		    result.put("token",token);
		    result.put("name", name);
		    result.put("role", roles);
		    
		
		    return new AjaxResult("登录成功", result);
		
	}
	
	@RequestMapping(value = "/loginFail")
	public Object loginFail() {
		  return new AjaxResult(false, "登录失败或用户无此权限，请重新登录");
	}
	
	@RequestMapping(value = "/logoutSucc")
	public Object logoutSucc() {
		return new AjaxResult("注销成功！");
	}
	
	
}
